__author__ = 'shawhen'

import ctypes

from .. import Kernel32
from .. import WinNT

from ..shared import minwindef as m_shared_minwindef


class MEMORYSTATUSEX(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
        ("dwLength", m_shared_minwindef.DWORD),
        ("dwMemoryLoad", m_shared_minwindef.DWORD),
        ("ullTotalPhys", WinNT.DWORDLONG),
        ("ullAvailPhys", WinNT.DWORDLONG),
        ("ullTotalPageFile", WinNT.DWORDLONG),
        ("ullAvailPageFile", WinNT.DWORDLONG),
        ("ullTotalVirtual", WinNT.DWORDLONG),
        ("ullAvailVirtual", WinNT.DWORDLONG),
        ("ullAvailExtendedVirtual", WinNT.DWORDLONG)
    ]

def GlobalMemoryStatusEx():
    """
    :return 一个MEMORYSTATUSEX结构体或者None; and GetLastError
    """
    memory_status_ex = MEMORYSTATUSEX()
    memory_status_ex.dwLength = ctypes.sizeof(MEMORYSTATUSEX)

    result = Kernel32.Kernel32_dll.GlobalMemoryStatusEx(ctypes.byref(memory_status_ex))
    if result == m_shared_minwindef.TRUE:
        return memory_status_ex
    elif result == m_shared_minwindef.FALSE:
        return None